<?php

namespace App\Http\Controllers\Admin;
use App\Http\Requests\Admin\Links\CreateRequest;
use App\Http\Requests\Admin\Links\UpdateRequest;
use App\Models\Admin\Links;
use App\Models\Admin\LinkSite;
use Illuminate\Http\Request;
use Style;

class LinksController extends CommonController{
    /**
     * 显示友情链接
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request){
        $input = arrayFilter($request->only(['keyword','link_site']));
        $links = Links::select('id','link_name','link_site','link_url','link_sort')
            ->where(function ($query) use ($input) {
                $keyword = (!empty($input['keyword'])) ? $input['keyword'] : '';
                if(strlen($keyword) > 0){
                    $query->where('link_name', 'like', '%'.$keyword.'%');
                }
            })->where(function ($query) use ($input) {
                $link_site = (!empty($input['link_site'])) ? $input['link_site'] : '';
                if(strlen($link_site) > 0){
                    $query->where('link_site',$link_site);
                }
            })
            ->orderBy('created_at','desc')
            ->paginate(20);
        if(count($links) > 0){
            foreach($links as $v){
                $v->link_site = LinkSite::where('id',$v->link_site)->first(['link_name'])->link_name;
                $v->url = $v->link_url;
            }
        }
        //dd($links);
        $link_site = LinkSite::get();
        return Style::view('admin.links.link_manage.index',compact('links','link_site','input'));
    }

    /**
     * 添加友情链接
     * @return mixed
     */
    public function create(){
        $link_site = LinkSite::select('id','link_name')->get();
        return Style::view('admin.links.link_manage.create',compact('link_site'));
    }

    /**
     * 添加友情链接提交
     * @param CreateRequest $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(CreateRequest $request){
        $input = $request->only(['link_name','link_site','link_url','link_sort']);
        $result = Links::create($input);
        if($result){
            flash('添加友情链接管理成功', 'success');
            return redirect('admin/links');
        }else{
            return back()->with('errors','友情链接管理添加失败，请稍后重试！');
        }
    }

    /**
     * 显示友情链接
     * @param $id
     */
    public function show($id){}

    /**
     * 编辑友情链接
     * @param $id
     * @return mixed
     */
    public function edit($id){
        $links = Links::find($id);
        $link_site = LinkSite::select('id','link_name')->get();
        return Style::view('admin.links.link_manage.edit',compact('links','link_site'));
    }

    /**
     * 更新友情链接
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function update(UpdateRequest $request, $id){
        $input = $request->only(['link_name','link_site','link_url','link_sort']);
        $result = Links::where('id',$id)->update($input);
        if($result){
            flash('更新友情链接管理成功', 'success');
            return redirect('admin/links');
        }else{
            return redirect()->back()->with('errors','更新前和更新后数据相同，更新失败！！');
        }
    }

    /**
     * 删除友情链接
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id){
        $result = Links::where('id',$id)->delete();
        if($result){
            return response()->json(['status' => 0,'msg' => '删除友情链接管理成功！']);
        }else{
            return response()->json(['status' => 1,'msg' => '删除友情链接管理失败，请稍后重试！']);
        }
    }
}
